﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autumn.Common;
using MoonLight.LibCommon;

//My Cmd Request=====================
//START
//TYPE: EXECUTE
//CMD: /home/limeng/cmd-tools/tools_src/server_lm/output.sh
//PARA: [arguments]
//END
//
//START
//TYPE: UPLOAD
//PATH_SRC: /home/limeng/cmd-tools/tools_src/server_lm/output.sh
//PATH_DST: /home/limeng/cmd-tools/tools_src/server_lm/output.sh
//CONTENT_LENGTH: MD5 string length
//CONTENT_BODY:
//MD5 string
//END
//
//START
//TYPE: DOWNLOAD
//PATH_SRC: /home/limeng/cmd-tools/tools_src/server_lm/output.sh
//PATH_DST: /home/limeng/cmd-tools/tools_src/server_lm/output.sh
//END
//
//My Cmd Response=====================
//START
//TYPE: EXECUTE_REP
//RST: [result]
//END
//
//START
//TYPE: UPLOAD_REP
//RST: [D][F]
//END
//
//START
//TYPE: DOWNLOAD_REP
//RST: [D][F]
//CONTENT_LENGTH: MD5 string length
//CONTENT_BODY:
//MD5 string
//END
//
//START
//TYPE: UNKNOWN
//RST: UNKNOWN
//END
//
/////////////////////////
namespace Autumn.Business
{
    class CmdHandler
    {
        public CmdHandler()
        {
            m_request = "";
            m_msg = new CmdMessage();
        }

        public int AddReq(string request)
        {
            if (request == null || request == "")
            {
                Logger.WriteLog("invalid request");
                return -1;
            }
            m_request = request;
            int ret = parse_req();
            return ret;
        }

        public ProjectDefine.CmdType_e GetCmdType()
        {
            return m_msg.type;
        }

        public string GetCmdPathSRC()
        {
            return m_msg.path_src;
        }

        public string GetCmdPathDST()
        {
            return m_msg.path_dst;
        }

        public string GetCmdCmd()
        {
            return m_msg.cmd;
        }

        public string GetCmdContent()
        {
            return m_msg.content;
        }

        public string GetCmdLength()
        {
            return m_msg.content_len;
        }

        public string GetCmdPara()
        {
            return m_msg.parameter;
        }

        private int parse_req()
        {
            string tmp = "";
            int start_idx = m_request.IndexOf("START", 0);
            if (start_idx == -1)
            {
                Logger.WriteLog("no START, invalid request");
                return -1;
            }
            //parse type
            tmp = "TYPE: ";
            int type_idx = m_request.IndexOf("TYPE", start_idx);
            if (type_idx == -1)
            {
                Logger.WriteLog("no TYPE, invalid request");
                return -1;
            }
            int type_end_idx = m_request.IndexOf("\n", type_idx) - 1;
            string type = m_request.Substring(type_idx + tmp.Length, type_end_idx - type_idx - tmp.Length + 1);
            Logger.WriteLog("type is [" + type + "]");
            if (type == "EXECUTE")
            {
                m_msg.type = ProjectDefine.CmdType_e.CMD_EXECUTE;
            }
            else if (type == "UPLOAD")
            {
                m_msg.type = ProjectDefine.CmdType_e.CMD_UPLOAD;
            }
            else if (type == "DOWNLOAD")
            {
                m_msg.type = ProjectDefine.CmdType_e.CMD_DOWNLOAD;
            }
            else
            {
                m_msg.type = ProjectDefine.CmdType_e.CMD_UNKNOWN;
            }
            Logger.WriteLog("req type [" + m_msg.type + "]");

            if (m_msg.type == ProjectDefine.CmdType_e.CMD_UPLOAD ||
                m_msg.type == ProjectDefine.CmdType_e.CMD_DOWNLOAD)
            {
                //parse path_src
                tmp = "PATH_SRC: ";
                int path_src_idx = m_request.IndexOf("PATH_SRC", start_idx);
                if (path_src_idx == -1)
                {
                    Logger.WriteLog("no PATH_SRC, invalid request");
                    return -1;
                }
                int path_src_end_idx = m_request.IndexOf("\n", path_src_idx) - 1;
                string path_src = m_request.Substring(path_src_idx + tmp.Length, path_src_end_idx - path_src_idx - tmp.Length + 1);
                m_msg.path_src = path_src;
                Logger.WriteLog("req path_src [" + m_msg.path_src + "]");

                //parse path_src
                tmp = "PATH_DST: ";
                int path_dst_idx = m_request.IndexOf("PATH_DST", start_idx);
                if (path_dst_idx == -1)
                {
                    Logger.WriteLog("no PATH_DST, invalid request");
                    return -1;
                }
                int path_dst_end_idx = m_request.IndexOf("\n", path_dst_idx) - 1;
                string path_dst = m_request.Substring(path_dst_idx + tmp.Length, path_dst_end_idx - path_dst_idx - tmp.Length + 1);
                m_msg.path_dst = path_dst;
                Logger.WriteLog("req path_dst [" + m_msg.path_dst + "]");
            }
            else if (m_msg.type == ProjectDefine.CmdType_e.CMD_EXECUTE)
            {
                //parse path_src
                tmp = "CMD: ";
                int cmd_idx = m_request.IndexOf("CMD", start_idx);
                if (cmd_idx == -1)
                {
                    Logger.WriteLog("no CMD, invalid request");
                    return -1;
                }
                int cmd_end_idx = m_request.IndexOf("\n", cmd_idx) - 1;
                string cmd = m_request.Substring(cmd_idx + tmp.Length, cmd_end_idx - cmd_idx - tmp.Length + 1);
                m_msg.cmd = cmd;
                Logger.WriteLog("req cmd [" + m_msg.cmd + "]");
            }

            //parse para
            tmp = "PARA: ";
            int para_idx = m_request.IndexOf("PARA", start_idx);
            if (para_idx == -1)
            {
                m_msg.parameter = "";
                Logger.WriteLog("no PARA");
            }
            else
            {
                int para_end_idx = m_request.IndexOf("\n", para_idx) - 1;
                string para = m_request.Substring(para_idx + tmp.Length, para_end_idx - para_idx - tmp.Length + 1);
                m_msg.parameter = para;
                Logger.WriteLog("req para [" + m_msg.parameter + "]");
            }

            //parse content_length
            tmp = "CONTENT_LENGTH: ";
            int length_idx = m_request.IndexOf("CONTENT_LENGTH", start_idx);
            if (length_idx != -1 && m_msg.type == ProjectDefine.CmdType_e.CMD_UPLOAD)
            {
                int length_end_idx = m_request.IndexOf("\n", length_idx) - 1;
                string length = m_request.Substring(length_idx + tmp.Length, length_end_idx - length_idx - tmp.Length + 1);
                m_msg.content_len = length;
                Logger.WriteLog("req content_len [" + m_msg.content_len + "]");
            }
            else if (length_idx == -1 && m_msg.type == ProjectDefine.CmdType_e.CMD_UPLOAD)
            {
                Logger.WriteLog("no CONTENT_LENGTH, invalid request");
                return -1;
            }
            else
            {

            }

            //parse content
            tmp = "CONTENT_BODY: \n";
            int body_idx = m_request.IndexOf("CONTENT_BODY", start_idx);
            if (body_idx != -1 && m_msg.type == ProjectDefine.CmdType_e.CMD_UPLOAD)
            {
                int body_end_idx = m_request.IndexOf("END", length_idx) - 2;
                string body = m_request.Substring(body_idx + tmp.Length, body_end_idx - body_idx - tmp.Length + 1);
                m_msg.content = body;
                Logger.WriteLog("req content_body [" + m_msg.content + "]");
            }
            else if (body_idx == -1 && m_msg.type == ProjectDefine.CmdType_e.CMD_UPLOAD)
            {
                Logger.WriteLog("no CONTENT_BODY, invalid request");
                return -1;
            }
            else
            {

            }
            return 0;
        }

        private string m_request;
        private CmdMessage m_msg;
    }

    class CmdMessage
    {
        public ProjectDefine.CmdType_e type;
        public string path_src;
        public string path_dst;
        public string cmd;
        public string content_len;
        public string content;
        public string parameter;
    }

    class ReponseFomator
    {
        public string GetResponse(string type, string result, string content)
        {
            string response = "";
            response += "START\n";
            response += "TYPE: " + type + "\n";
            response += "RST: " + result + "\n";
            response += "CONTENT_LENGTH: " + content.Length + "\n";
            response += "CONTENT_BODY: " + content + "\n";
            response += "END\n";
            return response;
        }
    }
}
